package top.italks.esp32app;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import top.italks.esp32app.bluetooth.BluetoothListActivity;
import top.italks.esp32app.common.BluetoothSocketThread;
import top.italks.esp32app.common.Constant;

public class MainActivity extends AppCompatActivity {
    private String deviceMac;
    private BluetoothSocketThread bluetoothSocketThread;
    private TextView TEMPVAL;
    private TextView AIRVAL;
    private TextView HUMIVAL;
    private Handler handler=new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    String messgae=(String) msg.obj;
                    if (messgae.contains("TEMPVAL")){
                        String value=messgae.substring(messgae.indexOf(":")+1,messgae.indexOf("\r\n"));
                        TEMPVAL.setText(value);
                    }else if (messgae.contains("AIRVAL")){
                        String value=messgae.substring(messgae.indexOf(":")+1,messgae.indexOf("\r\n"));
                        AIRVAL.setText(value);
                    }else if (messgae.contains("HUMIVAL")){
                        String value=messgae.substring(messgae.indexOf(":")+1,messgae.indexOf("\r\n"));
                        HUMIVAL.setText(value);
                    }
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TEMPVAL=(TextView) findViewById(R.id.TEMPVAL);
        AIRVAL=(TextView) findViewById(R.id.AIRVAL);
        HUMIVAL=(TextView) findViewById(R.id.HUMIVAL);
        findViewById(R.id.btBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(MainActivity.this, BluetoothListActivity.class),10);
            }
        });
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},10);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 10:
                deviceMac=data.getStringExtra("deviceMac");
                bluetoothSocketThread=new BluetoothSocketThread(deviceMac,handler);
                bluetoothSocketThread.start();
                break;
        }
    }

}